﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ZCrawler;
using NCrawler.Extensions;

namespace ZCrawlerManager
{
    public partial class FrmTask : DevExpress.XtraEditors.XtraForm
    {
        public DialogResult result;
        public ZTask task;
        private ZAnalysis tempAnalysis;
        private ZPublisher tempPublisher;
        public FrmTask()
        {
            InitializeComponent();
            result = System.Windows.Forms.DialogResult.Cancel;
            task = new ZTask();
            tempAnalysis = new ZAnalysis(new List<ZBasicRule>());
            this.txtTaskID.Text = task.TaskID;
        }
        public FrmTask(ZTask t)
        {
            InitializeComponent();
            result = System.Windows.Forms.DialogResult.Cancel;
            if (t == null)
            {
                task = new ZTask();
                return;
            }
            task = t;
            tempAnalysis = task.analysis;
            tempPublisher = task.publisher;
            this.txtTaskID.Text = task.TaskID;
            this.txtUrl.Text = task.Url;
            this.btnCSV.Text = task.CSVFile;
            if(task.pageSwitchAnalyst != null)
            {
                this.btnPageAnalysis.EditValue = task.pageSwitchAnalyst.Rules[0].Description();
            }
            if (task.navAnalyst != null)
            {
                this.btnNavAnalysis.EditValue = task.navAnalyst.Rules[0].Description();
            }
            gcField.DataSource = tempAnalysis.Rules;
            gvField.RefreshData();
        }

        private void btnPageAnalysis_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            using (FrmRuleSetting frm = task.pageSwitchAnalyst == null ? new FrmRuleSetting(tempAnalysis.Rules.Select(r => r.FieldName).ToArray(),txtUrl.Text) : new FrmRuleSetting(task.pageSwitchAnalyst.Rules[0], tempAnalysis.Rules.Select(r => r.FieldName).ToArray(), txtUrl.Text))
            {
                frm.ShowDialog();
                if (frm.result == System.Windows.Forms.DialogResult.OK)
                {
                    var rule = frm.rule;
                    ZAnalysis p_analysis = new ZAnalysis(rule);
                    p_analysis.IsPageUrlAnlysis = true;
                    task.pageSwitchAnalyst = p_analysis;
                    btnPageAnalysis.EditValue = rule.Description();
                }
            }
        }

        private void btnNavAnalysis_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            using (FrmRuleSetting frm = task.navAnalyst == null ? new FrmRuleSetting(tempAnalysis.Rules.Select(r => r.FieldName).ToArray(), txtUrl.Text) : new FrmRuleSetting(task.navAnalyst.Rules[0], tempAnalysis.Rules.Select(r => r.FieldName).ToArray(), txtUrl.Text))
            {
                frm.ShowDialog();
                if (frm.result == System.Windows.Forms.DialogResult.OK)
                {
                    var rule = frm.rule;
                    ZAnalysis n_analysis = new ZAnalysis(rule);
                    task.navAnalyst = n_analysis;
                    btnNavAnalysis.EditValue = rule.Description();
                }
            }
        }

        private void btnAddFieldAnalysis_Click(object sender, EventArgs e)
        {
            using(FrmRuleSetting frm = new FrmRuleSetting(tempAnalysis.Rules.Select(r => r.FieldName).ToArray(),txtUrl.Text))
            {
                frm.ShowDialog();
                if(frm.result == System.Windows.Forms.DialogResult.OK)
                {
                    var rule = frm.rule;
                    tempAnalysis.Rules.Add(rule);
                    gcField.DataSource = tempAnalysis.Rules;
                    gvField.RefreshData();
                }
            }
        }

        private void btnDeleteFieldAnalysis_Click(object sender, EventArgs e)
        {
            int handle = gvField.FocusedRowHandle;
            int dataSourceIndex = gvField.GetDataSourceRowIndex(handle);
            tempAnalysis.Rules.RemoveAt(dataSourceIndex);
            gcField.DataSource = tempAnalysis.Rules;
            gvField.RefreshData();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if(txtTaskID.Text.IsNullOrEmpty() || (txtUrl.Text.IsNullOrEmpty() && btnCSV.Text.IsNullOrEmpty()))
            {
                XtraMessageBox.Show("Please full fill fields");
                return;
            }
            if(tempAnalysis.Rules.Count == 0)
            {
                XtraMessageBox.Show("Please add at lease one crawl rule");
                return;
            }
            task.TaskID = txtTaskID.Text;
            task.Url = txtUrl.Text;
            task.CSVFile = btnCSV.Text;
            task.analysis = tempAnalysis;
            task.publisher = tempPublisher;
            if (btnCSV.EditValue != null)
                task.isCSVTask = !btnCSV.EditValue.ToString().IsNullOrEmpty();
            else
                task.isCSVTask = false;
            result = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            result = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void gvField_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Clicks == 2)
            {
                int handle = e.RowHandle;
                int index = gvField.GetDataSourceRowIndex(handle);
                ZBasicRule r = tempAnalysis.Rules[index];
                using (FrmRuleSetting frm = r == null ? new FrmRuleSetting(tempAnalysis.Rules.Select(rr => rr.FieldName).ToArray(),txtUrl.Text) : new FrmRuleSetting(r, tempAnalysis.Rules.Select(k => k.FieldName).ToArray(),txtUrl.Text))
                {
                    frm.ShowDialog();
                    if (frm.result == System.Windows.Forms.DialogResult.OK)
                    {
                        var rule = frm.rule;
                        tempAnalysis.Rules[index] = rule;
                        gcField.DataSource = tempAnalysis.Rules;
                        gvField.RefreshData();
                    }
                }
            }
        }

        private void btnPublisher_Click(object sender, EventArgs e)
        {
            if(tempAnalysis.Rules.Count == 0)
            {
                XtraMessageBox.Show("No rules detect");
                return; 
            }
            using(FrmPublish frm = new FrmPublish(tempPublisher,tempAnalysis.Rules.Select(r=>r.FieldName).ToArray()))
            {
                frm.ShowDialog();
                if(frm.result == System.Windows.Forms.DialogResult.OK)
                {
                    tempPublisher = frm.publisher;
                }
            }
        }

        private void btnCSV_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            using(OpenFileDialog dialog = new OpenFileDialog())
            {
                if(dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string csvName = dialog.FileName;
                    btnCSV.Text = csvName;
                }
            }
        }

        private void btnCSV_EditValueChanged(object sender, EventArgs e)
        {
            task.isCSVTask = btnCSV.Text.IsNullOrEmpty();
        }
    }
}